/**
 * learn redux functions
 * createStore 创建store
 * combineReducers 合并reducer
 * bindActionCreators 绑定action与方法，简化调用
 */

import React from 'react';
import { createStore, combineReducers, bindActionCreators } from 'redux';

//create reducer1
const counter = (state = { count: 0 }, action) => {
  switch (action.type) {
    case 'ADD':
      return { count: state.count + 1 }
    case 'REDUCE':
      return { count: state.count - 1 }
    default: 
      return state
  }
}
//create reducer2
const todos = (state={text:'吃饭'},action)=>{
  return state
}

//combine all reducers
const reducers = combineReducers({
  counter,
  todos
})


//create store[传入单个reducer或者reducers合集]
const store = createStore(reducers);


//create action
function add() {
  return {
    type: 'ADD'
  }
}

function reduce() {
  return {
    type: 'REDUCE'
  }
}

add = bindActionCreators(add, store.dispatch)
reduce = bindActionCreators(reduce, store.dispatch)

store.subscribe(() => { console.log(store.getState()) })

add();//store.dispatch('add')
reduce();//store.dispatch('reduce')
add()
reduce()

class C1 extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      count: 0,
    }
  }

  componentDidMount() {
    let localStore = localStorage.getItem('react_todolist');
    if (localStore) {
      this.setState({
        todolist: JSON.parse(localStore),
      })
    }
  }
 

  render() {
    return (
      <div>
        <h3>c1-pureRedux[check console result]</h3> 
      </div>
    )
  }
}


export default C1